/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Vocabulary;

import org.apache.log4j.Logger;
import org.myontology.users.Login;
import org.openrdf.model.URI;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 * Servlet implementation class for Servlet: OntologyExport
 *
 */
 public class ExportProperty extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
 	 
	private static final long serialVersionUID = 4; // do not know why...
	public static Logger logger = Logger.getLogger(ExportProperty.class);

	public ExportProperty() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		if ( !Login.LOGGED_IN.getValue(request) ) {			
			response.sendRedirect( request.getContextPath() + "/login");
			return;
		}
				
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		
		String ret = "";
		
		URI id = AbstractDataConnector.getURI( request.getParameter("id") );
		
	    PrintWriter out = response.getWriter();

		ret += ExportHelper.getHeader( request.getContextPath() );
	    
	    
	    // Main:
	    try {
	    	URI type =AbstractDataConnector.getType( id ); 
	    	
	    	if( type!=null ) {
		    	if( type.equals( Vocabulary.DATATYPEPROPERTY ) ) {
	
					ret += ExportHelper.getDatatypeProperty( id );
		    	} else if( type.equals( Vocabulary.OBJECTPROPERTY ) ) {
	
					ret += ExportHelper.getObjectProperty( id );
		    	}
	    }
	    
	    } catch( Exception e ) {
	    	logger.error( e );
	    }
	    
	    
	    
	    
	    ret +=
	    		"</rdf:RDF>\n";
	    
	    out.println( ret );
	}
	
}
